// 主要是对新版的webrtc进行改进
#include <iostream>
#include <string.h>
#include "webrtc_vad.h"
using namespace std;

int main(int argc, char *argv[]){
	FILE *fid = NULL;
	fid = fopen(argv[1], "rb");
	if (fid == NULL) {printf("open file is wrong\n"); return 0;}
	FILE *fidw = fopen("out.pcm", "wb");
	int fs = 48000;
	int perframe = 20; // ms
	fseek(fid, 0, SEEK_SET);	int far_begin = ftell(fid);
	fseek(fid, 0, SEEK_END);	int far_end = ftell(fid);
	int framelen = (int)fs/(1000/perframe);
	int frames = (far_end - far_begin)/2/framelen - 1;
	fseek(fid, 0, SEEK_SET);
	short int data[480*3] = {0}; // 最大支持48k-30ms
	//
	WebRtcVadInst *myvad = WebRtcVad_Create();
	WebRtcVad_Init(myvad);
	WebRtcVad_set_mode(myvad, 0);
	//
	for (int n=0; n<frames; n++){
		size_t val = fread(data, 2, framelen, fid);
		if (val != (size_t)framelen) break;
		int detector = WebRtcVad_Process(myvad, fs, data, framelen);
		fprintf(stderr, "frame = %d, %d\n",n, detector);
		for (int m=0; m<framelen; m++) fwrite(&detector, 2, 1, fidw);
	}
	// 判断是否是支持的采样率和帧长
	int judge = WebRtcVad_ValidRateAndFrameLength(fs, framelen);
	printf("support status is %d\n", judge);


	WebRtcVad_Free(myvad);
	fclose(fidw);
	fclose(fid);
	return 0;
}


